* ==============================================================================
* ==============================================================================
* IMF Book:	IMFBOOK_Ch2_Code.do
*			Chapter 2: The evolution of IMF conditionality
* ==============================================================================
use "IMFBOOK_Dataset.dta", clear
* ==============================================================================
* Generate variables
* ==============================================================================
encode ccode, gen(cid)
xtset cid year
foreach tru in BA1 BA2 {
	gen `tru'RFP = `tru'FP + `tru'RTP
	gen `tru'ENT = `tru'SOE + `tru'PRI
	gen `tru'GOV = `tru'ENV + `tru'INS + `tru'OTH + `tru'SP
} 
egen TBA1TOT = total(BA1TOT), by(cname)
egen TIMFnn = total(IMFnn), by(cname)
foreach var of varlist BA1TOT BA1DEB BA1FIN BA1RTP BA1EXT BA1ENT BA1LAB BA1GOV BA1POV {
	egen `var'_sum = total(`var'), by(year)
	bysort year: gen `var'_per = 100*(`var'_sum/BA1TOT_sum)
	gen `var'_dum = `var'>=1 & !missing(`var')
	egen `var'_dumtot = total(`var'_dum), by(year)
	gen `var'_dumprop = (`var'_dumtot/BA1TOT_dumtot)*100 
}
foreach var of varlist BA1TOT PAsTOT SPCsTOT SBsTOT SCsTOT QPCsTOT IBsTOT QCsTOT {
	egen `var'_av = mean(`var') if BA1TOT>0 & !missing(BA1TOT), by(year)
}
* ==============================================================================
* Factual statements
* ==============================================================================
* Mauritania had IMF programmes for 30 of the 40 years covered, carrying a total of 1,175 conditions.
list cname year BA1TOT TBA1TOT if ccode=="MRT" & year >= 1980 & year<=2019
* South Africa only had a one-year Stand-by Arrangement—the IMF’s staple lending facility—with 11 conditions attached between 1982-83. 
list cname year BA1TOT TBA1TOT if ccode=="ZAF" & year >= 1980 & year<=2019
* Of the 41 lending programmes active in 2019, 23 were to Sub-Saharan African countries. Lending to Europe and Central Asia, Latin America and the Caribbean, and East Asia and the Pacific—common in the past—has declined considerably. Only 11 countries from these three regions had IMF programmes in 2019, compared to 35 countries in 2000.
table region_WDI if year==2019 & BA1TOT>0 & !missing(BA1TOT)
table region_WDI if year==2000 & BA1TOT>0 & !missing(BA1TOT)
* Lithuania, with 417 conditions, held the median number of conditions out of countries that had at least one.
tabstat TBA1TOT if year==2019 & TBA1TOT>0, s(median)
sort TBA1TOT
list cname TBA1TOT if year==2019
* The total number of structural conditions reached 11.2 in 108, equivalent to the mean in the 2001-07 period.
tabstat SCsTOT if year>=1980 & year<=2019 & BA1TOT!=0 & !missing(BA1TOT), by(year) s(mean p50)
* ==============================================================================
* Table 2.1 Descriptive statistics on conditionality
* ==============================================================================
tabstat BA1TOT if year>=1980 & year<=2019 & BA1TOT>0 & !missing(BA1TOT), by(year) s(mean p50 n)
bysort year (BA1TOT): list cname BA1TOT if !missing(BA1TOT) & BA1TOT!=0
sort TBA1TOT
list cname TBA1TOT if year==2019
* ==============================================================================
* Table 2.2. Categorization of policy areas
* ==============================================================================
tabstat BA1DEB BA1FIN BA1RFP BA1EXT BA1ENT BA1LAB BA1GOV BA1POV BA1TOT if year>=1980 & year<=2019, column(statistics) stats(sum)
* ==============================================================================
* Figure 2.1. Total conditions, 1980-2019
* ==============================================================================
preserve
*** get the shapefiles in order
capture cd "GIS"
spshape2dta "World_Countries__Generalized_.shp", replace saving(world)
*** explore the outline file
use world_shp, clear
geo2xy _Y _X, proj (web_mercator) replace
*** merge with attributes file to get the names
merge m:1 _ID using world
drop if COUNTRY=="Antarctica"
drop rec_header- _merge
sort _ID
save, replace
*** merging
use world, clear
ren COUNTRY cname             
drop if cname=="Antarctica"
replace cname="Bahamas, The" if cname=="Bahamas"
replace cname="Congo, Dem. Rep." if cname=="Congo DRC"
replace cname="Congo, Rep." if cname=="Congo"
replace cname="Cote d'Ivoire" if cname=="Côte d'Ivoire"
replace cname="Egypt, Arab Rep." if cname=="Egypt"
replace cname="Gambia, The" if cname=="Gambia"
replace cname="Iran, Islamic Rep." if cname=="Iran"
replace cname="Korea, Dem. People's Rep." if cname=="North Korea"
replace cname="Korea, Rep." if cname=="South Korea"
replace cname="Kyrgyz Republic" if cname=="Kyrgyzstan"
replace cname="Lao PDR" if cname=="Laos"
replace cname="Micronesia, Fed. Sts." if cname=="Micronesia"
replace cname="Sint Maarten (Dutch part)" if cname=="Sint Maarten"
replace cname="Slovak Republic" if cname=="Slovakia"
replace cname="St. Kitts and Nevis" if cname=="Saint Kitts and Nevis"
replace cname="St. Lucia" if cname=="Saint Lucia"
replace cname="St. Martin (French part)" if cname=="Saint Martin"
replace cname="St. Vincent and the Grenadines" if cname=="Saint Vincent and the Grenadines"
replace cname="Syrian Arab Republic" if cname=="Syria"
replace cname="Venezuela, RB" if cname=="Venezuela"
replace cname="Virgin Islands (U.S.)" if cname=="US Virgin Islands"
replace cname="West Bank and Gaza" if cname=="Palestinian Territory"
replace cname="Yemen, Rep." if cname=="Yemen"
merge 1:m cname using "..\IMFBOOK_Dataset.dta"
*** see which countries didn't merge
tab cname if _m==1 
tab cname if _m==2 
*** generate a local for the w3 color scheme
colorpalette spmap greys, n(4) nograph
local colors `r(p)'
egen TBA1TOT = total(BA1TOT), by(cname)
replace TBA1TOT = . if TBA1TOT==0
spmap TBA1TOT using world_shp if year==2019, id(_ID) ocolor(black ..) osize(vvthin ..) fcolor("`colors'") legend(pos(7) size(*1)) legtitle("Number of conditions") legstyle(2) ndfcolor(white ..) ndlabel("None") ndsize(vvthin ..) clmethod(custom) clbreaks(1 350(350)1400)
graph export "..\Figures\IMFBOOK_Figure2-1.png", replace width(4000)
capture cd ..
restore
* ==============================================================================
* Figure 2.2. Mean of structural conditions in IMF programmes
* ==============================================================================
twoway (bar SCsTOT_av year, color(gray*0.5)) (tsline PAsTOT_av, lpattern(solid) lw(thick)) (tsline SPCsTOT_av, lpattern(shortdash_dot) lw(thick)) (tsline SBsTOT_av, lpattern(dash) lw(thick)) if year>=1980 & year<=2019, graphregion(color(white)) xtitle("") xlabel(1980(1)2019, angle(vertical) labsize(small)) ylabel(,angle(horizontal)) ytitle("Number of conditions") legend(lab(1 "Total") lab(2 "Prior Actions") lab(3 "Structural Performance Criteria") lab(4 "Structural Benchmarks")) scheme(s2mono)
graph export "Figures\IMFBOOK_Figure2-2.png", replace width(4000)
* ==============================================================================
* Figure 2.3. Conditionality in non-core policy areas
* ==============================================================================
graph bar BA1ENT_per BA1LAB_per BA1GOV_per BA1POV_per if year>=1980 & year<=2019, over(year, label(angle(vertical) labsize(small))) stack legend(label(1 "SOE reforms") label(2 "Labour issues") label(3 "Institutional reforms")  label(4 "Poverty reduction policies")) ytitle("Percent of conditions") ylabel(, angle(horizontal)) graphregion(color(white)) bar(1, lwidth(vthick)) bar(2, lwidth(vthick)) bar(3, lwidth(vthick)) bar(4, lwidth(vthick)) scheme(s2mono)
graph export "Figures\IMFBOOK_Figure2-3.png", replace width(4000)
* ==============================================================================
